package com.tsj.spzx.product.service.Impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tsj.spzx.model.dto.h5.ProductSkuDto;
import com.tsj.spzx.model.dto.product.SkuSaleDto;
import com.tsj.spzx.model.entity.product.Product;
import com.tsj.spzx.model.entity.product.ProductDetails;
import com.tsj.spzx.model.entity.product.ProductSku;
import com.tsj.spzx.model.vo.h5.ProductItemVo;
import com.tsj.spzx.product.mapper.ProductDetailsMapper;
import com.tsj.spzx.product.mapper.ProductMapper;
import com.tsj.spzx.product.mapper.ProductSkuMapper;
import com.tsj.spzx.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductImplService implements ProductService {

    @Autowired
    private ProductSkuMapper productSkuMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductDetailsMapper productDetailsMapper;

    /**
     * 从sku表查询畅销商品
     * 注意必须是上架的商品
     * @return
     */
    @Override
    public List<ProductSku> findProductBySale() {
        List<ProductSku> productSkuList = productSkuMapper.findProductBySale();
        return productSkuList;
    }

    /**
     * 根据条件分页查询商品数据
     * @param page
     * @param limit
     * @param productSkuDto
     * @return
     */
    @Override
    public PageInfo<ProductSku> findProductByPage(Integer page, Integer limit, ProductSkuDto productSkuDto) {
        PageHelper.startPage(page, limit);
        List<ProductSku> productSkuList = productSkuMapper.findProductByPage(productSkuDto);
        PageInfo<ProductSku> pageInfo = new PageInfo<>(productSkuList);
        return pageInfo;
    }

    /**
     * 根据skuId查询商品详情
     * @param skuId
     * @return
     */
    @Override
    public ProductItemVo findProductBySkuId(Long skuId) {
        // 1、现根据skuId从product_sku表查询商品sku信息
        ProductSku productSku = productSkuMapper.findProductBySkuId(skuId);
        // 2、根据productSku.productId从product表查询商品信息
        Long productId = productSku.getProductId();
        Product product = productMapper.findProductBySkuId(productId);
        // 3、根据productId从product_details表查询商品详情信息
        ProductDetails productDetails = productDetailsMapper.findProductBySkuId(productId);
        // 4、封装skuId和其应用的sku信息
        // 先查询同一个商品的所有sku信息
        Map<String, Object> skuSpecValueMap = new HashMap<>();
        List<ProductSku> productSkuList = productSkuMapper.findProductSku(productId);
        productSkuList.forEach(item -> {
            skuSpecValueMap.put(item.getSkuSpec(),item.getId());
        });

        // 封装数据
        ProductItemVo productItemVo = new ProductItemVo();
        productItemVo.setProduct(product);
        productItemVo.setProductSku(productSku);
        productItemVo.setSliderUrlList(Arrays.asList(product.getSliderUrls().split(",")));
        productItemVo.setDetailsImageUrlList(Arrays.asList(productDetails.getImageUrls().split(",")));
        productItemVo.setSkuSpecValueMap(skuSpecValueMap);
        productItemVo.setSpecValueList(JSON.parseArray(product.getSpecValue()));

        return productItemVo;
    }

    /**
     * 根据skuId获取商品sku信息
     * @param skuId
     * @return
     */
    @Override
    public ProductSku getBySkuId(Long skuId) {
        ProductSku productSku = productSkuMapper.findProductBySkuId(skuId);
        return productSku;
    }

    /**
     * 根据skuIdList查询商品sku信息
     * @param skuIdList
     * @return
     */
    @Override
    public List<ProductSku> findProductBySkuIdList(List<Long> skuIdList) {
        List<ProductSku> productSkuList = productSkuMapper.findProductBySkuIdList(skuIdList);
        return productSkuList;
    }

    /**
     * 支付成功后 更新商品sku信息
     * @param skuSaleDtoList
     */
    @Override
    public void updateProductSku(List<SkuSaleDto> skuSaleDtoList) {
        productSkuMapper.updateProductSku(skuSaleDtoList);
    }

}
